package com.wzk.travelspringboot.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzk.travelspringboot.dao.ScenicCommentDAO;
import com.wzk.travelspringboot.dao.ScenicCommentLikeDAO;
import com.wzk.travelspringboot.dao.ScenicDAO;
import com.wzk.travelspringboot.dao.UserDAO;
import com.wzk.travelspringboot.dto.Result;
import com.wzk.travelspringboot.dto.ScenicCommentDTO;
import com.wzk.travelspringboot.entity.Scenic;
import com.wzk.travelspringboot.entity.ScenicComment;
import com.wzk.travelspringboot.entity.ScenicCommentLike;
import com.wzk.travelspringboot.entity.User;
import com.wzk.travelspringboot.service.ScenicCommentService;
import com.wzk.travelspringboot.service.ScenicService;
import com.wzk.travelspringboot.service.UserService;
import com.wzk.travelspringboot.vo.ScenicCommentVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;

/**
 * @Author: WangZiKang
 * @Date: 2025/1/12 20:04
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ScenicCommentServiceImpl extends ServiceImpl<ScenicCommentDAO, ScenicComment> implements ScenicCommentService {

    private final ScenicDAO scenicDAO;

    private final UserDAO userDAO;

    private final ScenicCommentDAO commentDAO;

    private final ScenicCommentLikeDAO commentLikeDAO;

    @Override
    public Result comment(ScenicCommentDTO dto) {
        //1.判断用户是否存在
        User user = userDAO.selectById(dto.getUserId());
        if (user == null || user.getState() != 1){
            return Result.failed("此用户不存在或用户状态异常");
        }
        //2.判断该景区数据是否存在
        Scenic scenic = scenicDAO.selectById(dto.getScenicId());
        if (scenic == null){
            return Result.failed("景区数据不存在");
        }
        //3.添加评论
        ScenicComment comment = new ScenicComment();
        comment.setScenicId(dto.getScenicId());
        comment.setUserId(dto.getUserId());
        comment.setLike(0L);
        comment.setCreateTime(LocalDateTime.now());
        comment.setContent(dto.getContent());
        int i = commentDAO.addComment(comment);
        if (i <= 0){
            return Result.failed("评论失败!出现未知错误请类型管理员");
        }
        //4.返回添加成功的结果
        ScenicCommentVO vo = BeanUtil.copyProperties(comment, ScenicCommentVO.class);
        vo.setIsLiked(false);
        vo.setUsername(user.getNickName());
        vo.setAvatar(user.getAvatar());
        return Result.success("评论成功!",vo);
    }

    @Override
    public Result allCommentById(String id) {
        Long sid = Long.valueOf(id);
        //1.根据景区id查询该景区下所有的评论
        List<ScenicComment> comments = commentDAO.queryBySId(sid);
        //2.判断是否为空
        if (comments.isEmpty()){
            return Result.success("未找到任何数据!", Collections.emptyList());
        }
        //3.转换成vo并封装其他的参数
        ArrayList<ScenicCommentVO> vos = new ArrayList<>(comments.size());
        for (ScenicComment comment : comments) {
            ScenicCommentVO vo = BeanUtil.copyProperties(comment, ScenicCommentVO.class);
            User u = userDAO.selectById(comment.getUserId());
            if (u == null){
                vo.setUsername("未知用户");
            }else {
                vo.setUsername(u.getNickName());
                vo.setAvatar(u.getAvatar());
            }
            //查询当前用户是否点赞过
            long currUserId = 1;
            ScenicCommentLike like = commentLikeDAO.queryCurrUserIsLiked(comment.getId(), currUserId);
            if (like != null){
                vo.setIsLiked(true);
            }else {
                vo.setIsLiked(false);
            }
            vos.add(vo);
        }
        //4.返回结果
        return Result.success("评论加载成功!",vos);
    }
}
